<?php
$loader->requireOnce('lib/OFC/OFC_Chart.php');
abstract class PortletGReport {
	protected 	$reportId = '';
	protected 	$name     = '';
	protected 	$x_legend = '';
	protected 	$y_legend = '';
	protected 	$values   = '';
	protected 	$xLabels  = '';
	protected 	$yLabels  = '';
	protected 	$chart;
	protected 	$_db;
    protected   $maxValue = 0;

	public function __construct($reportId){
		$this->reportId	=	$reportId;
		$this->_db = $GLOBALS['frame']['adodb']['db'];
		$this->xLabels  =   array();
        $this->yLabels  =   array();
        $this->values   =   array();
	}

	public function renderChart(){
		$this->collectData();
		$this->buildChart();
		return $this->chart->toPrettyString();
	}

	abstract protected function collectData();
	abstract protected function buildChart();

	protected function getMaxY() {
        return ceil($this->maxValue/10)*10;
    }

    protected function getStepY() {
        $unit   =   10;
        $maxY =   self::getMaxY();
        $num  =   intval(count($this->values));
        if($num >  0) {
            $val   =   ceil($maxY/$num);
            $step   =   ceil($val/$unit)*$unit;
            if($step > $maxY/4) {
                $unit   =   5;
            	$val   =   ceil($maxY/$num);
                $step   =   ceil($val/$unit)*$unit;
            }
            if($step > $maxY/4) {
            	$unit   =   1;
                $val   =   ceil($maxY/$num);
                $step   =   ceil($val/$unit)*$unit;
            }
        }

        return intval($step);
    }
}
?>